package org.example.project.search.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**
 * 头部搜索卡片
 * @param text 搜索文本
 * @param isTop 是否在头部
 * @param hasText 存在搜索文本
 * @param searchClick 搜索点击事件
 * @param textChange 文本修改事件
 * @param clearText 清理文本事件
 */
@Composable
fun SearchCard(
    text: String,
    isTop: Boolean,
    hasText: Boolean,
    searchClick: () -> Unit,
    textChange: (it: String) -> Unit,
    clearText: () -> Unit
) {

    Column(
        modifier = Modifier
            .fillMaxWidth()
            .background(
                color = Color.White, shape = RoundedCornerShape(
                    topStart = 16.dp, topEnd = 16.dp,
                    bottomStart = if (isTop && !hasText) 0.dp else 16.dp,
                    bottomEnd = if (isTop && !hasText) 0.dp else 16.dp
                )
            ) // 设置背景色和圆角形状
            .padding(start = 12.dp, top = 8.dp, end = 12.dp, bottom = 8.dp),
    ) {
        BasicTextField(
            value = text,
            onValueChange = textChange,
            modifier = Modifier
                .fillMaxWidth()
                .background(Color.White)
                .height(51.dp),
            textStyle = TextStyle(fontSize = 16.sp, fontWeight = FontWeight.W400),
            maxLines = 1,
            decorationBox = { innerTextField ->
                Row(
                    modifier = Modifier
                        .padding(horizontal = 10.dp)
                        .fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Img(25.dp)
                    Spacer(modifier = Modifier.width(12.dp))
                    Box(
                        modifier = Modifier.weight(1f),
                        contentAlignment = Alignment.CenterStart
                    ) {
                        if (text.isEmpty())
                            Text(text = "武汉市近期活动", color = Color.LightGray, fontSize = 16.sp, fontWeight = FontWeight.W400)
                        innerTextField()

                    }
                    Spacer(modifier = Modifier.width(8.dp))
                    if (hasText) {
                        Img(18.dp, clearText)
                        Box(modifier = Modifier.padding(start = 10.dp).width(0.5.dp).height(15.dp).background(Color(0xaa000000)))
                    } else {
                        Img(25.dp)
                    }
                    Spacer(modifier = Modifier.width(10.dp))
                    Button(
                        modifier = Modifier
                            .width(55.dp)
                            .defaultMinSize(1.dp, 1.dp)
                            .padding(start = 0.dp),
                        contentPadding = PaddingValues(5.dp),
                        shape = RoundedCornerShape(16.dp),
                        onClick = searchClick,
                        colors = ButtonDefaults.buttonColors(
                            backgroundColor = Color(0xff2073FA),
                            contentColor = Color(0xff1C99F8)
                        )
                    ) {
                        Text(
                            text = "搜索",
                            color = Color.White,
                            fontWeight = FontWeight.W500,
                            fontSize = 16.sp,
                        )
                    }
                }
            }
        )

    }
}

